Free Hosting

Chars


Even though the char data type is an integer (and thus follows all of the normal integer rules), we typically work with chars in a different way than normal integers. Characters can hold either a small number, or a letter from the ASCII character set. ASCII stands for American Standard Code for Information Interchange, and it defines a mapping between the keys on an American keyboard and a number between 1 and 127 (called a code). For instance, the character ‘a’ is mapped to code 97. ‘b’ is code 98. Characters are always placed between single quotes.
The following two assignments do the same thing:
1
2
char chValue = 'a';
char chValue2 = 97;
cout outputs char type variables as characters instead of numbers.
The following snippet outputs ‘a’ rather than 97:
1
2
char chChar = 97; // assign char with ASCII code 97
cout << chChar; // will output 'a'
If we want to print a char as a number instead of a character, we have to tell cout to print the char as if it were an integer. We do this by using a cast to have the compiler convert the char into an int before it is sent to cout:
1
2
char chChar = 97;
cout << (int)chChar; // will output 97, not 'a'
The (int) cast tells the compiler to convert chChar into an int, and cout prints ints as their actual values. We will talk more about casting in a few lessons.
The following program asks the user to input a character, then prints out both the character and it’s ASCII code:
1
2
3
4
5
6
7
8
9
10
#include "iostream";
 
int main()
{
    using namespace std;
    char chChar;
    cout << "Input a keyboard character: ";
    cin >> chChar;
    cout << chChar << " has ASCII code " << (int)chChar << endl;
}
Note that even though cin will let you enter multiple characters, chChar will only hold 1 character. Consequently, only the first character is used.
One word of caution: be careful not to mix up character (keyboard) numbers with actual numbers. The following two assignments are not the same
1
2
char chValue = '5'; // assigns 53 (ASCII code for '5')
char chValue2 = 5; // assigns 5
Escape sequences
C and C++ have some characters that have special meaning. These characters are called escape sequences. An escape sequence starts with a \, and then a following letter or number.
The most common escape sequence is ‘\n’, which can be used to embed a newline in a string of text:
1
2
3
4
5
6
7
8
#include <iostream>
 
int main()
{
    using namespace std;
    cout << "First line\nSecond line" << endl;
    return 0;
}
This outputs:
First line
Second line
Another commonly used escape sequence is ‘\t’, which embeds a tab:
1
2
3
4
5
6
7
#include <iostream>
 
int main()
{
    using namespace std;
    cout << "First part\tSecond part";
}
Which outputs:
First part        Second part
Three other notable escape sequences are:
\’, which prints a single quote
\”, which prints a double quote
\\, which prints a backslash
Here’s a table of all of the escape sequences:
NameSymbolMeaning
Alert\aMakes an alert, such as a beep
Backspace\bMoves the cursor back one space
Formfeed\fMoves the cursor to next logical page
Newline\nMoves cursor to next line
Carriage return\rMoves cursor to beginning of line
Horizontal tab\tPrints a horizontal tab
Vertical tab\vPrints a vertical tab
Single quote\’Prints a single quote
Double quote\”Prints a double quote
Backslash\\Prints a backslash
Question mark\?Prints a question mark
Octal/hex number\(number)Translates into char represented by octal/hex number

0 comments:

Blogger Template by Clairvo